* Calculate weighted average K (country level) for each firm

* Dependencies: This file requires running
* firm_patents2.do -> generates patentsfam2.dta
* weights_wic3.do -> Generates weightswic3.dta


cd /users/andreas/dropbox/work/patstat/data
global tmp "/tmp"
global cutoff 1991 // 1991/2003/2008



* -----------------------
* New
* -----------------------
use patentsfam4, clear
drop if headq==".." 
drop if nace==.


collapse (sum) p, by(headq nace2_1 y)

* Expand data and fill in the zeros
egen id = group(headq nace)
tsset id y
tsfill, full  
replace p=0 if p==.
egen tmp = mode(headq), by(id)
drop headq
ren tmp headq
egen tmp = mode(nace2_1), by(id)
drop nace2_1
ren tmp nace2_1

* Cumulative sums
bys id: gen P = sum(p)
drop if y>2009 | y<1980
save $tmp/tmp, replace

collapse (sum) P, by(headq y)   // Add aggregate data as nace==99
gen nace2_1=99
gen lnP = log(P)
drop P
reshape wide lnP, i(headq nace2_1) j(y)
save $tmp/tmp2, replace

use $tmp/tmp, clear
drop if nace2_1==99
gen lnP = log(P)
drop P p
reshape wide lnP, i(headq nace2_1) j(y)
save $tmp/ttmpPreshapeNace, replace


* ----------------------------------------------
* Merge 
* ----------------------------------------------
use weightswic5_to1985, clear
*use weightswic5_to1975, clear
drop if headq==""
drop headq p tot

* Merge patent data
replace nace=99 if nace==.
ren appln headq
merge m:1 headq nace2_1 using $tmp/ttmpPreshapeNace, keep(match master)

* For some industries, we have missing data. Use the average P instead
replace nace=99 if _merge==1
drop _merge
merge m:1 headq nace2_1 using $tmp/tmp2, update 
drop if _merge==2
drop if lnP1992==.
drop _merge

* Adjust weights so they sum to 1 again
egen tmp = sum(w), by(hrm)
gen w2 = w/tmp
drop w tmp

sort hrm_l2_id
encode headq, gen(cty2)
drop headq

forvalues i=1980/2009 {
  gen lnPall`i' = lnP`i'*w2 
} 
collapse (sum) lnPall????, by(hrm)

*save lnPall_to1975, replace
save lnPall, replace


